﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VideoRentalShop.Model;
using VideoRentalShop.Controller;

namespace VideoRentalShop.View.Windows.OrderList
{
    /// <summary>
    /// Logika interakcji dla klasy SearchPanel.xaml
    /// </summary>
    public partial class SearchPanel : UserControl
    {
        private VideoRentalShopController _controller = VideoRentalShopController.Instance;


        public SearchPanel()
        {
            InitializeComponent();
            CreateListColumns();
            Reload();
        }


        private void CreateListColumns()
        {
            var binding = new MultiBinding { StringFormat = "{0} {1}" };
            binding.Bindings.Add(new Binding("LastName"));
            binding.Bindings.Add(new Binding("Name"));
            lstGridClients.Columns.Add(new GridViewColumn { Header = "Klient", DisplayMemberBinding = binding });

            lstGridMovies.Columns.Add(new GridViewColumn { Header = "Tytuł", DisplayMemberBinding = new Binding("Title") });
        }


        public void Reload()
        {
            ReloadClients();
            ReloadMovies();
        }

        private void ReloadClients()
        {
            lstClients.Items.Clear();
            foreach (var client in _controller.Data.Client.GetAll().OrderBy(x => new { x.LastName, x.Name }))
            {
                lstClients.Items.Add(new { Id = client.Id, Name = client.Name, LastName = client.LastName });
            }
        }

        private void ReloadMovies()
        {
            lstMovies.Items.Clear();
            foreach (var movie in _controller.Data.Movie.GetAll().OrderBy(x => x.Title))
            {
                lstMovies.Items.Add(new { Id = movie.Id, Title = movie.Title });
            }
        }


        public void Clear()
        {
            lstClients.SelectedItems.Clear();
            lstMovies.SelectedItems.Clear();
        }

        public IEnumerable<Order> GetResults()
        {
            IEnumerable<Order> result = _controller.Data.Order.GetAll();

            if (lstClients.SelectedItems.Count > 0)
            {
                var list = new List<long>();
                foreach (dynamic item in lstClients.SelectedItems)
                {
                    list.Add(item.Id);
                }

                result = result.Where(x => list.Contains(x.ClientId));
            }
            if (lstMovies.SelectedItems.Count > 0)
            {
                var list = new List<long>();
                foreach (dynamic item in lstMovies.SelectedItems)
                {
                    list.Add(item.Id);
                }

                var ids = _controller.Data.OrderDetails.GetAll()
                    .Where(x => list.Contains(x.MovieId))
                    .Select(x => x.OrderId)
                    .Distinct().ToList();

                result = result.Where(x => ids.Contains(x.Id));

            }

            return result;
        }
    }
}
